import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {RouterProvider} from 'react-router';
import {checkToken, setWatermark} from "../services/SecurityService.tsx";
import {getItem} from "../services/LocalDataService.tsx";
import Config from "../Config.tsx";
import {coreDispatch} from "../utils/StoreUtil.tsx";
import LoginRoute from "./LoginRoute.tsx";
import UnLoginRoute from "./UnLoginRoute.tsx";

// import GlobalDrawer from './module/GlobalDrawer';
// import GlobalModal from "./module/GlobalModal";


const AppRouter = () => {
    const [isLogin, setIsLogin] = useState<boolean | undefined>(undefined);

    useEffect(() => {
        coreDispatch("titleReducer/initTitleMap");
    }, []);

    const init = useCallback(async () => {
        const isLogin = !!getItem(Config.localToken);
        setIsLogin(isLogin);
        isLogin && checkToken();
        isLogin && setWatermark();
    }, []);

    useEffect(() => {
        init();
    }, []);

    const renderAppRouter = useMemo(() => {
        if (isLogin === undefined) {
            return null;
        }
        const routers = isLogin ? LoginRoute() : UnLoginRoute();
        return (
            <RouterProvider router={routers}/>
        )
    }, [isLogin]);
    return (renderAppRouter);
};

export default React.memo(AppRouter);